<template>
    <div>
        <h4>基础地图</h4>
        <div id="container"></div>
    </div>
</template>

<script setup>
import { onMounted, onUnmounted } from "vue";
import AMapLoader from "@amap/amap-jsapi-loader";

let map = null;

onMounted(() => {
  window._AMapSecurityConfig = {
    securityJsCode: "bfc4628de1efdb7a10d5a97be15d4cc1",
  };
  AMapLoader.load({
    key: "f8688af27c7f44f9904a27c6fa90a510", // 申请好的Web端开发者Key，首次调用 load 时必填
    version: "2.0", // 指定要加载的 JSAPI 的版本，缺省时默认为 1.4.15
    plugins: ["AMap.Scale"], //需要使用的的插件列表，如比例尺'AMap.Scale'，支持添加多个如：['...','...']
  })
    .then((AMap) => {
      map = new AMap.Map("container", {
          center: [121.045332, 31.19884],
    zoom: 8.8
      });
       function addPolygon(data) {
    let polygon = new AMap.Polygon({
      path: data,
      fillColor: '#ccebc5',
      strokeOpacity: 1,
      fillOpacity: 0.5,
      strokeColor: '#2b8cbe',
      strokeWeight: 1,
      strokeStyle: 'dashed',
      strokeDasharray: [5, 5],
    });
    polygon.on('mouseover', () => {
      polygon.setOptions({
        fillOpacity: 0.7,
        fillColor: '#7bccc4'
      })
    })
    polygon.on('mouseout', () => {
      polygon.setOptions({
        fillOpacity: 0.5,
        fillColor: '#ccebc5'

      })
    })
    map.add(polygon);
  }
  addPolygon(shanghai);
  addPolygon(suzhou);
  addPolygon(wuxi);
    })
    .catch((e) => {
      console.log(e);
    });
});

onUnmounted(() => {
  map?.destroy();
});
</script>

<style lang="scss" scoped>
  #container {
      width: 800px;
      height: 800px;
    }
</style>